﻿/**
 * ...
 * @author 
 */

package tab;

import flash.events.MouseEvent;
import flash.Lib;
import gui.hug.HUGSprite;
import gui.hug.HUGTabbedPanel;
import gui.hug.Tab;
import list.FavoritesEvent;
import list.SearchPanel;
import list.NewList;
import list.FavoriteNewList;
import list.OpenTabEvent;

import loader.Loader;

import client.Channel;

class FavoritesTab extends Tab
{
	private static inline var 	TITLE = "Избранное";
	
	private var loader   : 	Loader;
	private var newsList : 	FavoriteNewList;
	private var search   :  SearchPanel;
	private var OldData  :  Array<Channel>;
	private var sprite   :  HUGSprite;
	
	public function new(panel : HUGTabbedPanel = null, uid : Int) 
	{
		sprite = new HUGSprite();
		Loader.createInstance();
		loader = Loader.getInstance();
		var me = this;
		search = new SearchPanel(187, 0, function (text: String) { 
			if (text != "")
			me.getPanel().addTab(new FindInFavTab(text, me.panel));	
		} );
		newsList = new FavoriteNewList(4, Std.int(search.height) + 20, 600);
		
		sprite.addChild(search);
		sprite.addChild(newsList);		
		loader.getFavorites(onLoad);
		
		var onOpenTabEvent = function(e : OpenTabEvent) {
			var channel : Channel = cast(e.getContent(), Channel);
			var tab = new ItemsTab(channel.getChanId(), channel.getName(), panel, uid);
			tab.setId(channel.getChanId());
			panel.addTab(tab); 
		};
		
		newsList.addEventListener(OpenTabEvent.OPEN_TAB, onOpenTabEvent);
		//newsList.addEventListener(MouseEvent.MOUSE_WHEEL, Refresh);
		//newsList.addEventListener(FavoritesEvent.On, Refresh);
		super(sprite, TITLE, 1,panel, false);
	}
	
	public function onLoad(data : Array<Channel>) : Void	{
		for (channel in data)	{   
			newsList.addItem(channel,1);
		}
	}
	public function UpdLoad(data: Array<Channel>): Void		{
		var len = Std.int(newsList.getLength());
		var i : Int = 0;
		for (i in 0...len - 1)
			newsList.deleteItem(i);
		for (channel in data)	{   
			newsList.addItem(channel,1);
		}	
	}
	public function Refresh() {
	    loader.getFavorites(UpdLoad);
	}

}